package com.deepinjava.dbbmlite

class FirebirdProvider : BaseSqlProvider() {
    override val sysTimestamp = "CURRENT_TIMESTAMP"
    override val readPage = "FIRST #pagesize SKIP #offset"
    override val readTop = "ROWS 1 TO ?"
    override val instrLoc = "POSITION('#1#1#1#1',SCOL)"

    override val defINT = "int"
    override val defBIGINT = "bigint"
    override val defDOUBLE = "DOUBLE PRECISION"
    override val defDATETIME = "TIMESTAMP"

    override val updateSql : String
        get() {
            val pattern = """CONCAT ?\((.*),(.*?)\)"""
            return super.updateSql.replace(pattern.toRegex(), "$1||$2")
        }

    override val writeSql : String
        get() {
            val pattern = """CONCAT ?\((.*),(.*?)\)"""
            return  super.writeSql.replace(pattern.toRegex(), "$1||$2")
        }


    override fun getFindSql(i: Int): String {
        val pattern = """CONCAT ?\((.*),(.*?)\)"""
        return  super.getFindSql(i).replace(pattern.toRegex(), "$1||$2")
    }

    fun getCopySql (pagesize: Int, offset: Int): String{
        return copySql.replace("SELECT ","SELECT ${getReadPage(pagesize,offset)}",ignoreCase = true)
    }

    override fun getSumSql(i: Int): String {
        return super.getSumSql(i).replace("SUBSTRING(SCOL,1,$i)", "SUBSTRING(SCOL FROM 1 FOR $i)",ignoreCase = true)
    }
}

